package com.sl.au.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.sl.au.entity.BusinesssellerBaseInfo;
import com.sl.au.entity.BussinessLevel;
import com.sl.au.entity.Module;
import com.sl.au.entity.News;
import com.sl.au.entity.Part;
import com.sl.au.entity.User;
import com.sl.au.service.BusinesssellerBaseInfoService;
import com.sl.au.service.BussinessLevelService;
import com.sl.au.service.NewsService;
import com.sl.au.service.UserService;
import com.sl.utils.MsgHelper;

/**
 * 商户区域
 * 
 * @author yangdi
 *
 */
@RestController
@RequestMapping(value = "/ui/news")
public class NewsController extends BaseController {

		@Autowired
		NewsService newsService;
		
		@RequestMapping(method = RequestMethod.GET)
		public ModelAndView index(){
			ModelAndView modelAndView = this.createLayoutView("news/newsindex");
			return modelAndView;
		}
		
		@RequestMapping(value="addNews",method = RequestMethod.GET)
		public ModelAndView addNews(){
			ModelAndView modelAndView = this.createLayoutView("news/newsadd");
			String id = request.getParameter("id");
			if(id==null) {
				id="";
			}
			modelAndView.addObject("id", id);
			return modelAndView;
		}
		
		@RequestMapping(value = "getpage", method = RequestMethod.POST)
		public Map<String,Object> getPage(HttpServletRequest request) {
			Map<String,Object> map = new HashMap<String,Object>();
			int start = Integer.parseInt(request.getParameter("start"));
			int length = Integer.parseInt(request.getParameter("length"));
			String key=request.getParameter("key");
			List<News> modules=null;
			long count = 0;
			
			modules = newsService.getNewsByKey(start, length, key, null, null);
			count=newsService.getNewsByKeyCount(key);
			
			map.put("data", modules);
			map.put("draw", request.getParameter("draw"));
			map.put("recordsFiltered", count);
			map.put("recordsTotal", count);
			return map;
		}
		
		@RequestMapping(value="/save",method = RequestMethod.POST)
		public MsgHelper saveNews(@RequestBody News news){
			MsgHelper msgHelper = MsgHelper.success();
			User user = this.getMyAccount().getUser();
			news.setUser(user);
			newsService.savaNews(news);
			msgHelper.setMsg("新闻发布成功!");
			return msgHelper;
		};
		
		
		@RequestMapping(value = "saveNewsWithFile", method = RequestMethod.POST)
		public MsgHelper saveNewsWithFile(News news,@RequestParam(value = "file", required = false) MultipartFile file) {
			MsgHelper msgHelper = MsgHelper.success();
			if (file.getOriginalFilename() != null && !"".equals(file.getOriginalFilename())) {
				// 注意路径的构建
				String url = getUploadFilePath(this.fileUploadPath, "NewsPic", file);
				news.setNewsPicture(url);
			};
			User user = this.getMyAccount().getUser();
			news.setUser(user);
			newsService.savaNews(news);
			
			msgHelper.setMsg("新闻发布成功!");
			return msgHelper;
		}
		
		@RequestMapping("/delete/{id}")
		public MsgHelper deleteNews(@PathVariable("id") String id) {
			MsgHelper msgHelper = MsgHelper.success();
			newsService.getRepository().delete(id);
			return msgHelper;
		}
		
		@RequestMapping("/getOne/{id}")
		public Map<String,Object> getOne(@PathVariable("id") String id) {
			Map<String,Object> map = new HashMap<String,Object>();
			News news = newsService.getRepository().findOne(id);
			map.put("news", news);
			return map;
		}
		
}
